//
//  RCSearchCityListDataModel.m
//  RocketProject
//
//  Created by Wu Chengwei on 13-7-3.
//  Copyright (c) 2013年 Rocket. All rights reserved.
//

#import "RCSearchCityListDataModel.h"
#import "FrameworkRelativeDefines.h"
#import "NSArray+EX.h"

@implementation RCSearchCityListItem

@synthesize cityID = _cityID;
@synthesize cityName = _cityName;
@synthesize cityLetters = _cityLetters;
-(id)init{
    self = [super init];
    if (nil == self) {
        return nil;
    }
    _cityID = nil;
    _cityName = nil;
    return self;
}

-(void)dealloc{
    
    self.cityID= nil;
    self.cityName = nil;
    self.cityLetters = nil;
    [super dealloc];
}

+(id)parseDataFromJSON:(NSDictionary *)data{
    if (nil == data) {
        return nil;
    }
    RCSearchCityListItem *retObject = nil;
    retObject = [[RCSearchCityListItem alloc]init];
    if (nil == retObject) {
        return nil;
    }
    retObject.cityID = [data stringValueForKey:@"cityid" defaultValue:nil operation:NSStringOperationTypeTrim];
    if (retObject.cityID == nil) {
        //主要是针对机场类型数据
        retObject.cityID =[data stringValueForKey:@"threeword" defaultValue:nil operation:NSStringOperationTypeTrim];
    }
    retObject.cityName = [data stringValueForKey:@"cityname" defaultValue:nil operation:NSStringOperationTypeTrim];
    retObject.cityLetters = [data stringValueForKey:@"letters" defaultValue:nil operation:NSStringOperationTypeTrim];
    if (retObject.cityLetters == nil) {
        retObject.cityLetters = [data stringValueForKey:@"fletter" defaultValue:nil operation:NSStringOperationTypeTrim];

    }

    return [retObject autorelease];

}

@end

@implementation RCSearchCityListDataModel

@synthesize cityListArray = _cityListArray;

-(id)init{
    self = [super init];
    if (nil == self) {
        return nil;
    }
    _cityListArray = nil;
    return self;
}

-(void)dealloc{
    self.cityListArray = nil;
    [super dealloc];
}

+(id)parseDataFromJSON:(NSDictionary *)data{
    if (nil == data) {
        return nil;
    }
    RCSearchCityListDataModel *retObject = nil;
    retObject = [[RCSearchCityListDataModel alloc]init];
    if (nil == retObject) {
        return nil;
    }
    NSDictionary *dict = [data dictionaryValueForKey:@"data" defaultValue:nil];
    if (nil == dict) {
        return [retObject autorelease];
    }
    NSArray *listArray = [dict arrayValueForKey:@"list" defaultValue:nil];
    if (listArray == nil) {
        listArray = [dict arrayValueForKey:@"items" defaultValue:nil];

    }
    if (listArray != nil) {
        NSMutableArray *tempArray = [[NSMutableArray alloc]initWithCapacity:1];
        for (int i =0; i<[listArray count]; i++) {
            NSDictionary *itemDict = [listArray safeObjectAtIndex:i];
            if (nil == itemDict) {
                continue;
            }
            RCSearchCityListItem *item = [RCSearchCityListItem parseDataFromJSON:itemDict];
            if (item !=nil) {
                [tempArray addObject:item];
            }
            
        }
        retObject.cityListArray = [NSArray arrayWithArray:tempArray];
        [tempArray release];
    }
    return [retObject autorelease];
}

@end
